﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.ModelInterface;
using QLBH.StandardUI;

namespace QLBH.Category
{
    public partial class frmSelectProviderDlg : Form
    {
        private IQLNCCModel selectedNCC;

        public IQLNCCModel SelectedNCC
        {
            get { return selectedNCC; }
        }
        private List<IQLNCCModel> nccmodels;
        public frmSelectProviderDlg()
        {
            InitializeComponent();
            nccmodels = new List<IQLNCCModel>();
        }

        private void frmSelectProviderDlg_Load(object sender, EventArgs e)
        {
            LoadDSNCC();
        }

        private void LoadDSNCC()
        {
            List<NHACUNGCAP> nccs = NCCController.GetInstance().GetAll().ToList();            
            foreach (NHACUNGCAP ncc in nccs)
            {
                IQLNCCModel model = ModelFactory.ModelFactory.GetQLNCCModelInstance();
                model.InitFromEntity(ncc);

                nccmodels.Add(model);
            }
            dataGridViewEx1.DataSource = nccmodels;
        }

        private void frmSelectProviderDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                btnSelect_Click(btnSelect, null);
            }
            else if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(btnSearch, null);
            }
            else if (e.KeyCode == Keys.F2)
            {
                btnAddNew_Click(btnAddNew, null);
            }
            else if (e.KeyCode==Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            String customerName = tbxTenKH.Text;
            string sdt = tbxSDT.Text;

            if (customerName != "" && sdt != "")
            {
                var result = nccmodels.Where(kh => kh.TENNCC.Contains(customerName) && kh.DIDONG.Equals(sdt)).ToList();

                dataGridViewEx1.DataSource = result;

            }
            else if (customerName != "")
            {
                var result = nccmodels.Where(kh => kh.TENNCC.Contains(customerName)).ToList();

                dataGridViewEx1.DataSource = result;

            }
            else if (sdt != "")
            {
                var result = nccmodels.Where(kh => kh.DIDONG.Equals(sdt)).ToList();

                dataGridViewEx1.DataSource = result;
            }
            else
            {
                dataGridViewEx1.DataSource = nccmodels;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            SelectNCC();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNhaCungCapDlg addNewNCCdlg = new AddNhaCungCapDlg();
            if (addNewNCCdlg.ShowDialog() == DialogResult.OK)
            {
                LoadDSNCC();
            }
        }

        private void dataGridViewEx1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SelectNCC();
        }

        private void SelectNCC()
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                selectedNCC = dataGridViewEx1.SelectedRows[0].DataBoundItem as IQLNCCModel;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void dataGridViewEx1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                btnSelect.Enabled = true;
            }
            else
            {
                btnSelect.Enabled = false;
            }
        }
    }
}
